package com.neuedu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.CheWei;
import com.neuedu.model.DongShu;
import com.neuedu.service.CheWeiService;
import com.neuedu.service.impl.CheWeiServiceImpl;
import com.neuedu.util.R;
import com.neuedu.vo.PageVO;

public class CheWeiController {
    private CheWeiService cheWeiService=new CheWeiServiceImpl();

    public R page(JSONObject jsonObject){
        Integer pageNo=jsonObject.getInteger("pageNo");
        Integer pageSize=jsonObject.getInteger("pageSize");
        //在某两个日期之间，like 一个给定的车位名称。
        String startTime=jsonObject.getString("startTime");
        String endTime=jsonObject.getString("endTime");
        String name=jsonObject.getString("name");

        PageVO<CheWei> pageVO = cheWeiService.selectOnePage(pageNo, pageSize, startTime, endTime, name);
        return R.ok().setData(pageVO);
    }

    public R add(JSONObject jsonObject){
        String suoshuxiaoqu = jsonObject.getString("suoshuxiaoqu");
        String cheweimingcheng = jsonObject.getString("cheweimingcheng");
        Integer zhuangtai = jsonObject.getInteger("state");;

        CheWei cheWei=new CheWei();
        cheWei.setSuoshuxiaoqu(suoshuxiaoqu);
        cheWei.setCheweimingcheng(cheweimingcheng);
        cheWei.setZhuangtai(zhuangtai);

        //调用service，录入一个新的车位
        int result = cheWeiService.add(cheWei);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }
    }


    /**
     * 修改展示信息
     * @param jsonObject
     * @return
     */
    public R showCheWei(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        CheWei cheWei = cheWeiService.findCheWeiById(id);
        System.out.println(cheWei);
        return R.ok().setData(cheWei);
    }

    public R edit(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        CheWei cheWei = cheWeiService.findCheWeiById(id);
        String suoshuxiaoqu = jsonObject.getString("suoshuxiaoqu");
        String cheweimingcheng = jsonObject.getString("cheweimingcheng");
        Integer zhuangtai = jsonObject.getInteger("state");;
        cheWei.setSuoshuxiaoqu(suoshuxiaoqu);
        cheWei.setCheweimingcheng(cheweimingcheng);
        cheWei.setZhuangtai(zhuangtai);
        int result=cheWeiService.editCheWeiInfo(cheWei);
        if(result>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }
    }


    public R del(JSONObject jsonObject){
        JSONArray delId = jsonObject.getJSONArray("delId");
        Integer [] deleteId=new Integer[delId.size()];
        for (int i = 0; i < delId.size(); i++) {
            deleteId[i]=delId.getInteger(i);
        }

        int result = cheWeiService.del(deleteId);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }

    }
}
